global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area
global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 
adopath ++ "/bplimext/projects/p064_MathildeMunoz/tools"

********************************************
* Other Robustness cheks for Fig8-9
*******************************************

use "$outputs/ES/paneldata_ES.dta", clear
xtset tina ano
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=mean(E001), by(tina)
   drop if median<3
   drop if median==.
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
 

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob1/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(5.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_full_c`y'.png", replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_full_c`y'_cut.png", replace
}
}


foreach x in logcash logtangible {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob1/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(5.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_full_c`y'.png", replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_full_c`y'_cut.png", replace
}
}

keep if treated==1
egen clusterb1=group(time distrito)

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Rob1/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(5.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_ot_c`y'.png", replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob1/Figures/`x'_ot_c`y'_cut.png", replace
}
}

*****************************************************
* Robustness to balancing the panel around event time 
*****************************************************

use "$outputs/ES/paneldata_ES.dta", clear
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)

*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
*drop if ano<2008
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
    *balance the panel to firms observed in -1 and +1 for treated 
    gen tag1=.
    replace tag1=1 if time==-1 & treated==1
    egen tag1b=min(tag1), by(tina)
    drop if treated==1 & tag1b==.
     gen tag2=.
    replace tag2=1 if time==1 & treated==1
    egen tag2b=min(tag2), by(tina)
    drop if tag2b==. & treated==1
   
 

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob2/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob2/Figures/`x'_full_c`y'_cut.png", replace
}
}

foreach x in logcash logtangible {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob2/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob2/Figures/`x'_full_c`y'_cut.png", replace
}
}

keep if treated==1
egen clusterb1=group(time distrito)

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Rob2/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob2/Figures/`x'_ot_c`y'_cut.png", replace
}
}

*****************************************************
*. Robustness to balancing -2 years before 
*****************************************************

use "$outputs/ES/paneldata_ES.dta", clear
drop if cae3==31091
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
   *balance the panel to firms observed in -1 and +1 for treated 
    
    gen tag1=.
    replace tag1=1 if time==-1 & treated==1
    egen tag1b=min(tag1), by(tina)
    drop if treated==1 & tag1b==.
    
     gen tag2=.
    replace tag2=1 if time==1 & treated==1
    egen tag2b=min(tag2), by(tina)
    drop if tag2b==. & treated==1
   
    gen tag3=.
    replace tag3=1 if time==2 & treated==1
    egen tag3b=min(tag3), by(tina)
    drop if tag3b==. & treated==1
    
    gen tag4=.
    replace tag4=1 if time==-2 & treated==1
    egen tag4b=min(tag4), by(tina)
    drop if tag4b==. & treated==1
    
    
 

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob3/Estimates/`x'_full_c`y'.txt",replace

coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) xline(5.5, lcolor(red))
graph export "$outputs/ES/Baseline_Export5/Rob3/Figures/`x'_full_c`y'_cut.png", replace
}
}


foreach x in logcash logtangible {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob3/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) xline(5.5, lcolor(red))
graph export "$outputs/ES/Baseline_Export5/Rob3/Figures/`x'_full_c`y'_cut.png", replace
}
}


keep if treated==1
egen clusterb1=group(time distrito)

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Rob3/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) xline(5.5, lcolor(red))
graph export "$outputs/ES/Baseline_Export5/Rob3/Figures/`x'_ot_c`y'_cut.png", replace
}
}

******************************************************
*. Robustness to alternative selection on event-time 
*******************************************************

use "$outputs/ES/paneldata_ES.dta", clear
drop if cae3==31091
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)

*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
*drop if ano<2008
drop if event_d<2008 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   


foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob4/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Rob4/Figures/`x'_full_c`y'_cut.png", replace
}
}


foreach x in logcash logtangible {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob4/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob4/Figures/`x'_full_c`y'_cut.png", replace
}
}



 

keep if treated==1
egen clusterb1=group(time distrito)




foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Rob4/Estimates/`x'_ot_c`y'.txt",replace

coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(5.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob4/Figures/`x'_ot_c`y'.png", replace

coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Rob4/Figures/`x'_ot_c`y'_cut.png", replace
}
}


***********************************************
* Alternative median cutoff
**********************************************




use "$outputs/ES/paneldata_ES.dta", clear
drop if cae3==31091
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)

*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
*drop if ano<2008
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<5
   drop if median==.
   
   *egen mediand=median(D001), by(tina)
   *drop if mediand<100
   *drop if mediand==.
   
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
 

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob6/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Rob6/Figures/`x'_full_c`y'_cut.png", replace
}
}


foreach x in logcash logtangible {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Rob6/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Rob6/Figures/`x'_full_c`y'_cut.png", replace
}
}


keep if treated==1
egen clusterb1=group(time distrito)




foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Rob6/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Rob6/Figures/`x'_ot_c`y'_cut.png", replace
}
}


**********************************************
* Semi-dynamic and full dynamic robustness
*********************************************
   

use "$outputs/ES/paneldata_ES.dta", clear
xtset tina ano
drop if cae3==31091

g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)
g loghourstemp=log(E018)
g loghourspart=log(E017)
g logssc=log(DL013)
g logtax=log(D060)
g logdebt=log(B081)


*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(ano distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
*drop if ano<2008
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   


reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store full
reghdfe logwage  zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store semi
reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb(i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store treated
reghdfe logwage zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb(i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store treatedsemi
reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb( i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store nofe



coefplot (full, label("Full Sample- Dynamic")) (semi, label("Full Sample-Semi Dynamic") ) (treated, label("Treated Sample- Dynamic")) (treatedsemi, label("Treated Sample- Semi Dynamic")) (nofe, label("Treated Sample- No Firm FE"))  , keep(coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) recast(connected) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(size(vsmall) nobox region(lcolor(white)))
graph export "$outputs/ES/Baseline_Export5/Figures/logwage_stacked.png", replace

reghdfe logEBT coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store EBTfull
reghdfe logEBT  zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store EBTsemi
reghdfe logEBT coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb(i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store EBTtreated
reghdfe logEBT zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb(i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store EBTtreatedsemi
reghdfe logEBT coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1, absorb( i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster1)
est store EBTnofe


coefplot (EBTfull, label("Full Sample- Dynamic")) (EBTsemi, label("Full Sample-Semi Dynamic") ) (EBTtreated, label("Treated Sample- Dynamic")) (EBTtreatedsemi, label("Treated Sample- Semi Dynamic")) (EBTnofe, label("Treated Sample- No Firm FE"))  , keep(coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) recast(connected) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(size(vsmall) nobox region(lcolor(white)))
graph export "$outputs/ES/Baseline_Export5/Figures/logEBT_stacked.png", replace



**********************************************
* Heterogeneous treatment effetcs
********************************************


global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area
global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 
adopath ++ "/bplimext/projects/p064_MathildeMunoz/tools"

*note: version of multipledid installed in 2020 by Bplim (no automatic updates in the server) 

clear 
use "$outputs/ES/paneldata_ES.dta", clear 
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(MG002)
g logassets=log(B001)
g logservices=log(MG011)
g treati=0
replace treati=1 if treated==1 & time>-1
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)

g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1

drop if event_d<2008 & treated==1
egen group=group(event_d)

global depvar E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal

forval y=2006/2017{
g dyear_`y'=0
replace dyear_`y'=1 if ano==`y'
}

foreach y in $list {
g dsect_`y'=0
replace dsect_`y'=1 if cae3==`y'
}

egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.

   
forval y=1/1{
did_multiplegt logemp group ano treati,  placebo(4) breps(20) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logemp_multipledid_c`y'.pdf", replace

did_multiplegt loghours group ano treati,  placebo(4) breps(20) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Multiplegt/loghours_full_c`y'.pdf", replace

did_multiplegt logservices group ano treati,  placebo(4) breps(20) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Multiplegt/logservices_full_c`y'.pdf", replace

did_multiplegt logwage group ano treati,  placebo(4) breps(40) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logwage_multipledid_c`y'.pdf", replace

did_multiplegt logwagebill group ano treati,  placebo(4) breps(20) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Multiplegt/logwagebill_full_c`y'.pdf", replace

did_multiplegt logca group ano treati,  placebo(4) breps(10) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Multiplegt/logca_full_c`y'.pdf", replace

did_multiplegt logEBT group ano treati,  placebo(4) breps(40) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logEBT_multipledid_c`y'.pdf", replace

did_multiplegt logassets group ano treati,  placebo(4) breps(20) dynamic(4) cluster(cluster`y')
graph export "$outputs/ES/Multiplegt/logassets_full_c`y'.pdf", replace

}

   
   
forval y=1/1{
did_multiplegt logemp group ano treati,  placebo(4) breps(20) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logemp_multipledid_c`y'.pdf", replace

*did_multiplegt loghours group ano treati,  placebo(4) breps(20) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
*graph export "$outputs/ES/Multiplegt/loghours_full_c`y'.pdf", replace

*did_multiplegt logservices group ano treati,  placebo(4) breps(20) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
*graph export "$outputs/ES/Multiplegt/logservices_full_c`y'.pdf", replace

did_multiplegt logwage group ano treati,  placebo(4) breps(40) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logwage_multipledid_c`y'.pdf", replace

*did_multiplegt logwagebill group ano treati,  placebo(4) breps(20) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
*graph export "$outputs/ES/Multiplegt/logwagebill_full_c`y'.pdf", replace

*did_multiplegt logca group ano treati,  placebo(4) breps(10) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
*graph export "$outputs/ES/Multiplegt/logca_full_c`y'.pdf", replace

did_multiplegt logEBT group ano treati,  placebo(4) breps(40) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export4/Rob12/Figures/logEBT_multipledid_c`y'.pdf", replace


*did_multiplegt logassets group ano treati,  placebo(4) breps(20) dynamic(4) trends_nonparam(dsect_* distrito )  controls(dyear_2006 dyear_2007 dyear_2008 dyear_2009 dyear_2010 dyear_2011 dyear_2012 dyear_2013 dyear_2014 dyear_2015 dyear_2016 dyear_2017 ) cluster(cluster`y')
*graph export "$outputs/ES/Multiplegt/logassets_full_c`y'.pdf", replace

}









